package com.rzgeo.wcgallay.comp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

/**
 * Created by zhanghang on 2018/1/8.
 */

public class WcmPanel extends FrameLayout {

    private View _foldBtn;
    private OnClickListener _onFoldBtn = new OnClickListener() {
        @Override
        public void onClick(View v) {
            WcmPanel.this.setVisibility(GONE);
            _foldBtn.setVisibility(GONE);
        }
    };

    public WcmPanel(@NonNull Context context) {
        super(context);
    }

    public WcmPanel(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public WcmPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    View[] _subViews = null;

    /***
     *
     * @param id -1位隐藏
     */
    public void setOnSelect(int id) {
        if (_subViews == null) {
            _subViews = new View[getChildCount()];

            for (int i = 0; i < getChildCount(); i++) {
                _subViews[i] = getChildAt(i);
            }
        }
        boolean findIdHere = false;
        for (View v : _subViews) {
            if (v.getId() == id) {
                findIdHere = true;
            }
        }
        if (findIdHere) {
            for (View v : _subViews) {
                if (v.getId() == id) {
                    if (v.getVisibility() != VISIBLE)
                        v.setVisibility(VISIBLE);
                    if (this.getVisibility() != VISIBLE)
                        this.setVisibility(VISIBLE);
                    _foldBtn.setVisibility(VISIBLE);
                }else{
                    if (v.getVisibility() != GONE)
                        v.setVisibility(GONE);
                }
            }
        }
    }

    public void setup(View foldBtn) {
        _foldBtn = foldBtn;
        _foldBtn.setVisibility(getVisibility());
        _foldBtn.setOnClickListener(_onFoldBtn);

    }
}
